package com.easy.sftp.pool;

import com.easy.sftp.config.SftpConfig;
import com.easy.sftp.config.ShellConfig;
import com.easy.sftp.config.TerminalAutoConfig;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelShell;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.jmx.support.RegistrationPolicy;

import javax.annotation.Resource;

/**
 * @author jetlag
 * @date 2022/9/16
 **/
@Configuration
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class Channel {

    @Resource
    private TerminalAutoConfig terminalAutoConfig;

    @Bean
    @ConditionalOnProperty(name = "easy.terminal.sftp.turnOn", havingValue = "true")
    public GenericObjectPool<ChannelSftp> channelSftp() {
        return new Pool<SftpConfig, ChannelSftp>(terminalAutoConfig.getSftp()).getPool();
    }

    @Bean
    @ConditionalOnProperty(name = "easy.terminal.shell.turnOn", havingValue = "true")
    public GenericObjectPool<ChannelShell> channelShell() {
        return new Pool<ShellConfig, ChannelShell>(terminalAutoConfig.getShell()).getPool();
    }


}
